$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'tea/scientific/query',
        datatype: "json",
        colModel: [
            { label: 'ID', name: 'id',hidden: true, width: 30, key: true },
            { label: '教研室', name: 'deptname', width: 60 },
            { label: '工号', name: 'num', width: 40 },
            { label: '姓名', name: 'username', width: 40 },
            { label: '项目成果名称', name: 'scientificName', width: 60 },

            { label: '转化金额（万元）', name: 'price', width: 40 },
            { label: '转化方式', name: 'kindName', width: 40 },
            { label: '参与学生数', name: 'stuCount', width: 40 }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,//div 的隐藏和显示
        dmoptions:[],//部门的集合
        p: {
            deptId:"",
            username:""
        },
        title: null,
        kdoptions:[],//类别的集合

        scientific: {
            id:null,
            scientificName: "",
            price: "",
            scientificKindId: "",
            stuCount: ""

        }
    },
    created:function(){
        $.get(baseURL+"sys/dept/list",function (r) {

            vm.dmoptions=r;
        });
    },
    methods: {

        query: function () {

            vm.reload();
        },
        excel: function(){
            // var str=vm.getDataStr();
            window.location.href=baseURL + "sys/scientific/exportExcel?dept_id="+vm.p.deptId;
        },

        add: function(){
            vm.scientific={
                id:null,
                scientificName: "",
                price: "",
                scientificKindId: "",
                stuCount: ""

            };
            vm.showList = false;
            vm.title = "新增";
            $.get(baseURL+"tea/dict/list/scientific",function (r) {

                vm.kdoptions=r;
            });


        },
        saveOrUpdate: function (event) {
            var url = "tea/scientific/saveOrUpdate";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.scientific),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                        vm.reload();
                    }
                }
            });
        },
        del: function () {
            var id = getSelectedRows();
            if(id == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "tea/scientific/delete",
                    contentType: "application/json",
                    data: JSON.stringify(id),
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                            vm.reload();
                        }
                    }
                });
            });
        },
        edit: function(){
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.showList = false;
            vm.title = "修改";
            //$.ajaxSettings.async = false;
            $.get(baseURL + "tea/scientific/edit/"+id+"/scientific", function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.scientific = r.edit;
                vm.kdoptions=r.kdoptions;
            });



        },








        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            page ="1";
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'deptId':vm.p.deptId,'username':vm.p.username},
                page:page
            }).trigger("reloadGrid");
        }
    }
});